package edu.gatech.cs4400.spring2014.A8.model;

import java.sql.Date;

public abstract class DataCleaner {
	
	/**
	 * Clean the input string to form a 10 digit phone number
	 * If the string is invalid, return the empty string
	 * 
	 * @param number
	 * @return
	 */
	public static String phoneNumber(String number) {
		String result = "";
		if (number != null) {
			number = number.replaceAll("\\D", ""); //remove all non-digits
			if (number.matches("[0-9]{10}")) {
				return number;
			}
		}
		return result;
	}
	
	public static Date date(String date) {
		Date result = null;
		try {
			result = Date.valueOf(date);
		}
		catch (IllegalArgumentException e) {
			result = Date.valueOf("2014-04-10");
		}
		return result;
	}
	
	public static int nonnegativeInt(String num) {
		int val = 0;
		try {
			val = Integer.parseInt(num);
			if (val < 0) {
				val = 0;
			}
		}
		catch (NumberFormatException e) {
		}
		return val;
	}
	
	public static float nonnegativeFloat(String num) {
		float val = 0;
		try {
			val = Float.parseFloat(num);
			if (val < 0) {
				val = 0;
			}
		}
		catch (NumberFormatException e) {
		}
		return val;
	}
}
